/**
 * 
 */
package com.tomagency.cyberlab.server.rpc;

import org.apache.log4j.Logger;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.tomagency.cyberlab.client.rpc.UserService;
import com.tomagency.cyberlab.server.config.FileSystemConfig;
import com.tomagency.cyberlab.server.util.LoggingUtil;
import com.tomagency.cyberlab.shared.LoginResult;

/**
 * @author gfr
 *
 */
@Singleton
public class UserServiceServlet extends RemoteServiceServlet implements
		UserService {
	
	public  static final String USER_NAME="cyberlab";
	public  static final String PASSWORD="cy1070!51!";
	
	public  static final String ADMIN_NAME="gfradmin";
	public  static final String ADMIN_PASSWORD="schottengasse!50+1";
	
	public  static final String GUEST_NAME="gast";
	public  static final String GUEST_PASSWORD="bubu";
	
	@Inject private LoggingUtil loggingUtil;
	@Inject private FileSystemConfig fsystem;
 
	
	/**
	 * 
	 */
	public UserServiceServlet() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param delegate
	 */
	public UserServiceServlet(Object delegate) {
		super(delegate);
		// TODO Auto-generated constructor stub
	}

	/* (non-Javadoc)
	 * @see com.tomagency.cyberlab.client.rpc.UserService#doLogin(java.lang.String, java.lang.String)
	 */
	@Override
	public LoginResult doLogin(String username, String pw) {
		Logger accessLogger = loggingUtil.getAccessLogger(UserServiceServlet.class.getName());
		accessLogger.info("Login from remote-ip="+this.getThreadLocalRequest().getRemoteAddr());
		LoginResult lr = new LoginResult();
		lr.setValidUser(false);
		lr.setUserName(username);
		lr.setMessage("Username: "+username+ " Fehler beim Login");
		
		if (USER_NAME.equalsIgnoreCase(username)&&PASSWORD.equalsIgnoreCase(pw)
				||ADMIN_NAME.equalsIgnoreCase(username)&&ADMIN_PASSWORD.equalsIgnoreCase(pw)
				||GUEST_NAME.equalsIgnoreCase(username)&&GUEST_PASSWORD.equalsIgnoreCase(pw)){
			accessLogger.info("successful login from remote-ip="+this.getThreadLocalRequest().getRemoteAddr()+ " user: "+username);
			
			lr.setValidUser(true);
			lr.setUserName(username);
			lr.setMessage(username+", willkommen beim Cyberlab Order-System");
			
		}
		else{
			accessLogger.info("wrong login attempt from remote-ip="+this.getThreadLocalRequest().getRemoteAddr()+ " user: "+username);

			lr.setValidUser(false);
			lr.setUserName(username);
			lr.setMessage("für \""+username+ "\" falsches Passwort angegeben");
		}

		return lr;
	}

}
